package com.netease.financial.base.lockpattern;

import android.support.annotation.NonNull;

import com.netease.financial.common.util.preferences.PreferencesHelper;

/**
 * Created by wyf on 7/27/15.
 */
public class LockPatternPreferencesHelper {

    private static final String TAG = LockPatternPreferencesHelper.class.getSimpleName();

    private static final String PREFERENCES_FILE_NAME = PreferencesHelper.getFullPreferenceFileName("lock_pattern");

    private static final String KEY_LAST_APP_ENTER_BACKGROUND_TIME = "last_app_enter_background_time";
    private static final String KEY_LOGIN_TIME_OUT = "login_time_out";
    private static final String KEY_APP_IN_BACKGROUND = "app_in_background";

    private static final String KEY_CLIENT_ID = "clientId";
    private static final String KEY_USER_ID = "userId";

    private static final String KEY_PREFIX_LOCK_PATTERN_SALT = "lock_pattern_salt_";

    private static final String KEY_FAILED_ATTEMPT_TIMEOUT_MILLIS = "failed_attempt_timeout_millis";
    private static final String KEY_FAILED_ATTEMPTS_SINCE_LAST_TIMEOUT = "failed_attempts_since_last_timeout";

    @NonNull
    private static String getSaltKey(String userId) {
        return KEY_PREFIX_LOCK_PATTERN_SALT + "user_" + userId;
    }

    private static PreferencesHelper sPreferencesHelper;

    private static PreferencesHelper getPreferencesHelper() {
        if (sPreferencesHelper == null) {
            sPreferencesHelper = PreferencesHelper.newInstance(PREFERENCES_FILE_NAME);
        }
        return sPreferencesHelper;
    }

    public static void setLastAppEnterBackgroundTime(long timeMills) {
        getPreferencesHelper().putLong(KEY_LAST_APP_ENTER_BACKGROUND_TIME, timeMills);
    }

    public static long getLastAppEnterBackgroundTime() {
        return getPreferencesHelper().getLong(KEY_LAST_APP_ENTER_BACKGROUND_TIME, System.currentTimeMillis());
    }

    public static void setLoginTimeOut(boolean needLock) {
        getPreferencesHelper().putBoolean(KEY_LOGIN_TIME_OUT, needLock);
    }

    public static boolean isLoginTimeOut() {
        return getPreferencesHelper().getBoolean(KEY_LOGIN_TIME_OUT, false);
    }

    public static long getSalt(String userId) {
        return getPreferencesHelper().getLong(getSaltKey(userId), 0);
    }

    public static void setSalt(long salt, String userId) {
        getPreferencesHelper().putLong(getSaltKey(userId), salt);
    }

    public static void setAppInBackground(boolean inBackground) {
        getPreferencesHelper().putBoolean(KEY_APP_IN_BACKGROUND, inBackground);
    }

    public static boolean isAppFromBackground() {
        return getPreferencesHelper().getBoolean(KEY_APP_IN_BACKGROUND, false);
    }

    public static void setUserId(String userId) {
        getPreferencesHelper().putString(KEY_USER_ID, userId);
    }

    public static String getUserId() {
        return getPreferencesHelper().getString(KEY_USER_ID, "");
    }

    public static void setClientId(String clientId) {
        getPreferencesHelper().putString(KEY_CLIENT_ID, clientId);
    }

    public static String getClientId() {
        return getPreferencesHelper().getString(KEY_CLIENT_ID, "");
    }

    public static long getFailedAttemptTimeoutMillis() {
        return getPreferencesHelper().getLong(KEY_FAILED_ATTEMPT_TIMEOUT_MILLIS, 0);
    }

    public static void setFailedAttemptTimeoutMillis(long millis) {
        getPreferencesHelper().putLong(KEY_FAILED_ATTEMPT_TIMEOUT_MILLIS, millis);
    }

    public static int getFailedAttemptsSinceLastTimeout() {
        return getPreferencesHelper().getInt(KEY_FAILED_ATTEMPTS_SINCE_LAST_TIMEOUT, 0);
    }

    public static void setFailedAttemptsSinceLastTimeout(int count) {
        getPreferencesHelper().putInt(KEY_FAILED_ATTEMPTS_SINCE_LAST_TIMEOUT, count);
    }
}
